package com.qf.iot.service;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.iot.Mapper.QfTenantMapper;
import com.qf.iot.core.vo.R;
import com.qf.iot.entity.QfTenant;
import com.qf.iot.utils.GetJwtToken;
import org.springframework.stereotype.Service;


/**
 * 作者：肖骏麟
 * 时间:2023/10/27 11:04
 * 描述：优化小子
 */
@Service
public class QfTenantService extends ServiceImpl<QfTenantMapper,QfTenant> {
    public R login(QfTenant qfTenant) {
        if (qfTenant == null) {
            return R.fail("非法参数");
        }
        QueryWrapper<QfTenant> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(QfTenant::getName,qfTenant.getName());
        QfTenant qfTenantDb = this.baseMapper.selectOne(queryWrapper);

        if (qfTenantDb == null) {
            return R.fail("用户名或者密码不正确");
        }

        //比较密码
        String md5 = SecureUtil.md5(qfTenant.getPwd());
        if(!md5.equals(qfTenantDb.getPwd())){
            return R.fail("密码不正确");
        }
        //颁发jwt令牌
        qfTenantDb.setPwd("");
        String token = GetJwtToken.getTokenExpireInHour(qfTenantDb, 2);
        return R.ok(token,"success");
    }


}
